#include <iostream>
#include <map>
#include <utility>
#include <functional>
#include <algorithm>
#include <fstream>
#include <string>
using namespace std;

#define STDIN
//#define DEBUG

class State {
    public:
        int m;
        int n;

        State(int m) {
            this->m = m;
        }

        bool next_case() {
            if (m--)
                return true;
            else
                return false;
        }

        void set_n(int n) {
            this->n = n;
        }

        bool go_next() {
            if (n--) 
                return true;
            else
                return false;
        }
};

class Direction {
    public:
        int x, y, z;
        Direction(int x, int y, int z):
            x(x), y(y), z(z) {
            }

        int getAxis() const{
            if (x == 1)
                return 0;
            else if (x == -1)
                return 3;
            else if (y == 1)
                return 1;
            else if (y == -1)
                return 4;
            else if (z == 1)
                return 2;
            else if (z == -1)
                return 5;
            else
                cerr << "Invalid Direction!!!" << endl;
            return -1;
        }

        Direction& operator=(const Direction& d) {
            if (this == &d)
                return *this;
            x = d.x;
            y = d.y;
            z = d.z;
            return *this;
        }


        Direction& operator-() {
            x = -x;
            y = -y;
            z = -z;
            return *this;
        }

        friend ostream& operator<<(ostream& os, const Direction& d);

        friend Direction left_hand(Direction& face, Direction& head);
        friend Direction right_hand(Direction& face, Direction& head);
};

ostream& operator<<(ostream& os, const Direction& d) {
    cout << "("<< d.x << "," << d.y << "," << d.z << ")";
    return os;
}

Direction left_hand(Direction& face, Direction& head)
{
    int x = face.y * head.z - face.z * head.y;
    int y = face.z * head.x - face.x * head.z;
    int z = face.x * head.y - face.y * head.x;
    return Direction(x, y, z);
}

Direction right_hand(Direction& face, Direction& head)
{
    int x = face.y * head.z - face.z * head.y;
    int y = face.z * head.x - face.x * head.z;
    int z = face.x * head.y - face.y * head.x;
    return Direction(-x, -y, -z);
}


class Astronaut {
    public:

        int x, y, z;
        Direction face;
        Direction head;

        Astronaut(int x, int y, int z) :
            x(x), y(y), z(z), face(1, 0, 0), head(0, 0, 1) {
            }

        void go(int count) {
            switch(face.getAxis()) {
                case 0:
                    x += count;
                    break;
                case 1:
                    y += count;
                    break;
                case 2:
                    z += count;
                    break;
                case 3:
                    x -= count;
                    break;
                case 4:
                    y -= count;
                    break;
                case 5:
                    z -= count;
                    break;
                default:
                    cerr << "Invalid Direction!!!" << endl;
            }
        }

        void left(int count) {
            face = left_hand(face, head);
            go(count);
        }
        void right(int count) {
            Direction l = right_hand(face, head);
            face = l;
            go(count);
        }
        void up(int count) {
            Direction f = head;
            head = -face;
            face = f;
            go(count);
        }
        void down(int count) {
            Direction h = head;
            head = face;
            face = -h;
            go(count);
        }
        void forward(int count) {
            go(count);
        }
        void back(int count) {
            face = -face;
            go(count);
        }

        friend ostream& operator<<(ostream& os, const Astronaut& as);
};

ostream& operator<<(ostream& os, const Astronaut& as) {
#ifdef DEBUG
    os << "坐标(" << as.x << "," << as.y << "," << as.z << ")"
        << "\t前方: " << as.face << "\t上方: " << as.head
        << endl;
#else
    os << as.x << " " << as.y << " " << as.z << " " << as.face.getAxis() << endl;
#endif
    return os;
}

typedef void (Astronaut::*PFUN)(int);

typedef map<string, void (Astronaut::*)(int)> STRING_OPS_MAP;

class Operation {
    public:
        map<string, void (Astronaut::*)(int)> op;

        Operation() {
            op.insert(STRING_OPS_MAP::value_type("left", &Astronaut::left));
            op.insert(STRING_OPS_MAP::value_type("right", &Astronaut::right));
            op.insert(STRING_OPS_MAP::value_type("up", &Astronaut::up));
            op.insert(STRING_OPS_MAP::value_type("down", &Astronaut::down));
            op.insert(STRING_OPS_MAP::value_type("forward", &Astronaut::forward));
            op.insert(STRING_OPS_MAP::value_type("back", &Astronaut::back));
        }
        void go(Astronaut& as,string& order_str, int count) {
#ifdef DEBUG
            std::cout << "order: " << order_str << "\tstep: " << count << std::endl;
#endif
            map<string, void (Astronaut::*)(int)>::iterator it;
            it =    op.find(order_str);
            if (it == op.end())
                cerr << "Invalid orders" << endl;
            else {
                PFUN pfun = (*it).second;
                (as.*pfun)(count);
            }
        }

};

int
main() {
#ifdef STDIN
    ifstream cin;
    cin.open("bin/1835.in");
#endif

    int m, n;
    cin >> m;

    State state(m);


    string  order_str;
    int     cnt;
    while(state.next_case()) {
        cin >> n;
        state.set_n(n);

        Astronaut as(0, 0, 0);
        Operation op;

        while(state.go_next())
        {
            cin >> order_str;
            cin >> cnt;
            op.go(as, order_str, cnt);;
#ifdef DEBUG
            cout << "结果: \n" << as  << endl;
#endif
        }
        cout << as;
    }
    return 0;
}
